﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace TCM.Controllers
{
    public class LevelController : Controller
    {
        private TCMContext db = new TCMContext();

        //
        // GET: /Level/

        public ActionResult Index()
        {
            var Levels = db.Levels.Include(l => l.RecordState);
            return View(Levels.ToList());
        }

        //
        // GET: /Level/Details/5

        public ActionResult Details(Guid? id = null)
        {
            Level Level = db.Levels.Find(id);
            if (Level == null)
            {
                return HttpNotFound();
            }
            return View(Level);
        }

        //
        // GET: /Level/Create

        public ActionResult Create()
        {
            ViewBag.StatusID = new SelectList(db.RecordStates, "StatusID", "StatusName");
            ViewBag.CreatedBy = new SelectList(db.UserProfiles, "USERID", "UserName");
            ViewBag.SubjectID = new SelectList(db.Subjects, "SubjectID", "Name");
            ViewBag.ModifiedBy = new SelectList(db.UserProfiles, "USERID", "UserName");
            ViewBag.OwnerID = new SelectList(db.UserProfiles, "USERID", "UserName");
            return View();
        }

        //
        // POST: /Level/Create

        [HttpPost]
        
        public ActionResult Create(Level Level)
        {
            if (ModelState.IsValid)
            {
                Level.LevelID = Guid.NewGuid();
                db.Levels.Add(Level);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            ViewBag.StatusID = new SelectList(db.RecordStates, "StatusName", "StatusID", Level.StatusID);
            ViewBag.CreatedBy = new SelectList(db.UserProfiles, "USERID", "UserName", Level.CreatedBy);
            ViewBag.ModifiedBy = new SelectList(db.UserProfiles, "USERID", "UserName", Level.ModifiedBy);
            ViewBag.SubjectID = new SelectList(db.Subjects, "SubjectID", "Name");
            ViewBag.OwnerID = new SelectList(db.UserProfiles, "USERID", "UserName", Level.OwnerID);
            return View(Level);
        }

        //
        // GET: /Level/Edit/5

        public ActionResult Edit(Guid? id = null)
        {
            Level Level = db.Levels.Find(id);
            if (Level == null)
            {
                return HttpNotFound();
            }
            ViewBag.StatusID = new SelectList(db.RecordStates, "StatusName", "StatusID", Level.StatusID);
            ViewBag.CreatedBy = new SelectList(db.UserProfiles, "USERID", "NAME", Level.CreatedBy);
            ViewBag.ModifiedBy = new SelectList(db.UserProfiles, "USERID", "NAME", Level.ModifiedBy);
            ViewBag.SubjectID = new SelectList(db.Subjects, "SubjectID", "Name");
            ViewBag.OwnerID = new SelectList(db.UserProfiles, "USERID", "NAME", Level.OwnerID);
            return View(Level);
        }

        //
        // POST: /Level/Edit/5

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit(Level Level)
        {
            if (ModelState.IsValid)
            {
                db.Entry(Level).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.StatusID = new SelectList(db.RecordStates, "StatusName", "StatusID", Level.StatusID);
            ViewBag.CreatedBy = new SelectList(db.UserProfiles, "USERID", "NAME", Level.CreatedBy);
            ViewBag.ModifiedBy = new SelectList(db.UserProfiles, "USERID", "NAME", Level.ModifiedBy);

            ViewBag.SubjectID = new SelectList(db.Subjects, "SubjectID", "Name");
            ViewBag.OwnerID = new SelectList(db.UserProfiles, "USERID", "NAME", Level.OwnerID);
            return View(Level);
        }

        //
        // GET: /Level/Delete/5

        public ActionResult Delete(Guid? id = null)
        {
            Level Level = db.Levels.Find(id);
            if (Level == null)
            {
                return HttpNotFound();
            }
            return View(Level);
        }

        //
        // POST: /Level/Delete/5

        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteConfirmed(Guid id)
        {
            Level Level = db.Levels.Find(id);
            db.Levels.Remove(Level);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}